package com.glory.brepda.activity.technicalservice;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.ToolBindActivity;
import com.glory.brepda.activity.recipe.ProgramDownLoadActivity;
import com.glory.brepda.activity.recipe.TERecipeDBWBActivity;
import com.glory.brepda.adapter.ReplaceEqpListAdapter;
import com.glory.brepda.adapter.ReplaceMaterialListAdapter;
import com.glory.brepda.adapter.ReplaceToolListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.MaterielListEntity;
import com.glory.brepda.entity.RepairFromEntity;
import com.glory.brepda.entity.RepairFromListEntity;
import com.glory.brepda.entity.RepairSheetDataEntity;
import com.glory.brepda.entity.ToolEntity;
import com.glory.brepda.entity.ToolListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReplaceProductActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvEqp)
    TextView tvEqp;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvPriority)
    TextView tvPriority;
    @BindView(R.id.tvComment)
    TextView tvComment;
    @BindView(R.id.tvOwner)
    TextView tvOwner;
    @BindView(R.id.tvClazz)
    TextView tvClazz;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.list_titleLayout)
    LinearLayout listTitleLayout;
    @BindView(R.id.rvReplaceMaterialList)
    RecyclerView rvReplaceMaterialList;
    @BindView(R.id.rvReplaceToolList)
    RecyclerView rvReplaceToolList;
    @BindView(R.id.tvAddTool)
    TextView tvAddTool;
    @BindView(R.id.tvStartRepair)
    TextView tvStartRepair;
    @BindView(R.id.tvProgram)
    TextView tvProgram;
    @BindView(R.id.tvTERecipe)
    TextView tvTERecipe;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llBtnLayout)
    LinearLayout llBtnLayout;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;
    private ReplaceEqpListAdapter mReplaceEqpListAdapter;
    private RepairFromListEntity.RepairData mData;
    private ReplaceMaterialListAdapter mMaterialAdapter;
    private ReplaceToolListAdapter mToolAdapter;
    private String mFrom = "";


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_replace_product;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("换产品");
        tvBaseSubmit.setVisibility(View.GONE);
        initRecyclerView(rvReplaceMaterialList, this);
        initRecyclerView(rvReplaceToolList, this);
        initRecyclerView(rvEqpList, this);
        mData = (RepairFromListEntity.RepairData) getIntent().getExtras().getSerializable("REPAIRFROM");
        mFrom = getIntent().getExtras().getString("FROM");
        tvEqp.setText(mData.getREPAIRSHEETDATA().getEQUIPMENTID());
        tvEqpType.setText(mData.getREPAIRSHEETDATA().getLOCATION());
        tvComment.setText(mData.getREPAIRFORM().getCOMMENTS());
        tvLot.setText(mData.getREPAIRSHEETDATA().getLOTID());
        tvClazz.setText(mData.getREPAIRSHEETDATA().getSHIFT());
        tvOwner.setText(mData.getREPAIRSHEETDATA().getOWNER());
        tvPriority.setText(mData.getREPAIRSHEETDATA().getPRIORITY());
        etComment.setText(mData.getREPAIRSHEETDATA().getREPAIRCOMMENT());
        tvStartRepair.setText(mData.getREPAIRFORM().getSTATUS().equals("START") ? "开始调机" : "调机完成");
        mMaterialAdapter = new ReplaceMaterialListAdapter(null);
        mMaterialAdapter.isFirstOnly(true);
        mMaterialAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mMaterialAdapter.setStart(mData.getREPAIRFORM().getSTATUS().equals("START"));
        rvReplaceMaterialList.setAdapter(mMaterialAdapter);
        mMaterialAdapter.setEqpID(tvEqp.getText().toString());
        mToolAdapter = new ReplaceToolListAdapter(null);
        mToolAdapter.isFirstOnly(true);
        mToolAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvReplaceToolList.setAdapter(mToolAdapter);
        mToolAdapter.setStart(mData.getREPAIRFORM().getSTATUS().equals("START"));
        mToolAdapter.setEqp(tvEqp.getText().toString());
        tvAddTool.setVisibility(mData.getREPAIRFORM().getSTATUS().equals("START") ? View.GONE : View.VISIBLE);
        if (mFrom != null && mFrom.equals("QC")){
            updateUI();
        }
        mReplaceEqpListAdapter = new ReplaceEqpListAdapter(null);
        mReplaceEqpListAdapter.isFirstOnly(true);
        mReplaceEqpListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvEqpList.setAdapter(mReplaceEqpListAdapter);
        mReplaceEqpListAdapter.setNewData(mData.getEQPMATERIALS());
    }

    private void updateUI() {
        tvBaseSubmit.setVisibility(View.GONE);
        llBtnLayout.setVisibility(View.GONE);
        tvAddTool.setVisibility(View.GONE);
        etComment.setEnabled(false);
        mToolAdapter.setStart(true);
        mMaterialAdapter.setStart(true);
    }

    @Override
    protected void initData() {
        if (!actionControls("TERecipeDBWBActivity")) {
            tvTERecipe.setVisibility(View.VISIBLE);
        }

        if (!actionControls("ProgramDownLoadActivity")) {
            tvProgram.setVisibility(View.VISIBLE);
        }

        FreeUI_EntityCallBack<BaseResponseBean<MaterielListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MaterielListEntity>>
                (new TypeToken<BaseResponseBean<MaterielListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MaterielListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<MaterielListEntity.Materiel> mlotlist = response.body().getResponse().getBody().getMLOTLIST();
                    mMaterialAdapter.setNewData(mlotlist);

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MaterielListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"MLOTKITTINGQUERY");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","LOAD");
        map.addProperty("EQUIPMENTID",tvEqp.getText().toString());
//        NetUtils.requestPostNet("IF.MLOTKITTINGQUERY","MLOTKITTINGQUERY",this,map,callBack);

        FreeUI_EntityCallBack<BaseResponseBean<ToolListEntity>> callBack1 = new FreeUI_DialogEntityCallBack<BaseResponseBean<ToolListEntity>>
                (new TypeToken<BaseResponseBean<ToolListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ToolListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<ToolEntity> toollist = response.body().getResponse().getBody().getTOOLLIST();
                    mToolAdapter.setNewData(toollist);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ToolListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TOOLREPORT");
            }
        };
        JsonObject map1 = new JsonObject();
        map1.addProperty("ACTIONTYPE","LOAD");
        map1.addProperty("EQUIPMENTID",tvEqp.getText().toString());
//        NetUtils.requestPostNet("IF.TOOLREPORT","TOOLREPORT",this,map1,callBack1);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    @OnClick({R.id.tvStartRepair, R.id.tvTERecipe, R.id.tvProgram,R.id.tvAddTool})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvStartRepair:
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    if (tvStartRepair.getText().toString().equals("开始调机")) {
                                        showToast("开始调机成功", true);
                                        tvStartRepair.setText("调机完成");
                                        mMaterialAdapter.setStart(false);
                                        mToolAdapter.setStart(false);
                                        tvAddTool.setVisibility(View.VISIBLE);
                                        mData.getREPAIRFORM().setSTATUS("STARTFORM");
                                        mData.getREPAIRSHEETDATA().setSTARTREPAIRTIME(FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"));
                                        mData.getREPAIRSHEETDATA().setREPAIROWNER(Constant.CURRENT_USER);
                                    } else {
                                        showConfirmDialog(ReplaceProductActivity.this, "提交成功");
                                    }

                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(), "PMSREPAIR");

                            }
                        };

                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", tvStartRepair.getText().toString().equals("开始调机") ? "STARTFORM" : "EXECUTE");
                        map.addProperty("REPAIRTYPE", "changeProducct");
                        //2023.11.21技质部三次检验没改好，一分厂先跑起来，把QC部分剔除
                        map.addProperty("QC", rbYes.isChecked()?"Y":"N");
                        mData.getREPAIRFORM().setOWNER(Constant.CURRENT_USER);
                        RepairFromEntity repairform = mData.getREPAIRFORM();
                        map.add("REPAIRFORM", new JsonParser().parse(new Gson().toJson(repairform)).getAsJsonObject());
                        RepairSheetDataEntity repairsheetdata = mData.getREPAIRSHEETDATA();
                        if (tvStartRepair.getText().toString().equals("调机完成")){
                            repairsheetdata.setREPAIRCOMMENT(getText(etComment));
                        }
                        map.add("REPAIRSHEETDATA", new JsonParser().parse(new Gson().toJson(repairsheetdata)).getAsJsonObject());
                        NetUtils.requestPostNet("IF.PMSREPAIR", "PMSREPAIR", ReplaceProductActivity.this, map, callBack);
                    }
                });

                break;
            case R.id.tvTERecipe:
                FreeApi_Utils.skipWithExtraIntent("header", this, TERecipeDBWBActivity.class);
                break;
            case R.id.tvProgram:
                FreeApi_Utils.skipWithExtraIntent("header", this, ProgramDownLoadActivity.class);
                break;
            case R.id.tvAddTool:
                FreeApi_Utils.skipWithExtraIntent("header",this, ToolBindActivity.class);
                break;
        }
    }
}
